/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using NUnit.Framework;

namespace OpenServers.Net.Common.FileSystem.Virtual {
    [TestFixture]
    public class VirtualFileSystemTest {
        [Test]
        public void TestGetDirectoryRoot() {
            VirtualDirectory root = new VirtualDirectory(null, null);
            VirtualFileSystem fs = new VirtualFileSystem(root);
            IDirectory dir = fs.GetDirectory("/");
            Assert.AreEqual(root, dir);
        }

        [Test]
        public void TestGetDirectoryOneLevel() {
            VirtualDirectory root = new VirtualDirectory(null, null);
            VirtualDirectory childDir = new VirtualDirectory(root, "abc");
            root.GetItems().Add(childDir);
            VirtualFileSystem fs = new VirtualFileSystem(root);
            IDirectory dir = fs.GetDirectory("/abc/");
            Assert.AreEqual(childDir, dir);
        }

        [Test]
        public void TestGetDirectoryMultilevel() {
            VirtualDirectory root = new VirtualDirectory(null, null);
            VirtualDirectory child1Dir = new VirtualDirectory(root, "abc");
            VirtualDirectory child2Dir = new VirtualDirectory(child1Dir, "def");
            root.GetItems().Add(child1Dir);
            child1Dir.GetItems().Add(child2Dir);
            VirtualFileSystem fs = new VirtualFileSystem(root);
            IDirectory dir = fs.GetDirectory("/abc/def");
            Assert.AreEqual(child2Dir, dir);
        }

        [Test]
        public void TestGetFileRoot() {
            VirtualDirectory root = new VirtualDirectory(null, null);
            VirtualFile file = new VirtualFile(root, "a.txt");
            root.GetItems().Add(file);
            VirtualFileSystem fs = new VirtualFileSystem(root);
            IFile found = fs.GetFile("/a.txt");
            Assert.AreEqual(file, found);
        }

        [Test]
        public void TestGetFileOneLevel() {
            VirtualDirectory root = new VirtualDirectory(null, null);
            VirtualDirectory childDir = new VirtualDirectory(root, "abc");
            root.GetItems().Add(childDir);
            VirtualFile file = new VirtualFile(root, "a.txt");
            childDir.GetItems().Add(file);
            VirtualFileSystem fs = new VirtualFileSystem(root);
            IFile found = fs.GetFile("/abc/a.txt");
            Assert.AreEqual(file, found);
        }

        [Test]
        public void TestGetFileMultilevel() {
            VirtualDirectory root = new VirtualDirectory(null, null);
            VirtualDirectory child1Dir = new VirtualDirectory(root, "abc");
            VirtualDirectory child2Dir = new VirtualDirectory(child1Dir, "def");
            root.GetItems().Add(child1Dir);
            child1Dir.GetItems().Add(child2Dir);
            VirtualFile file = new VirtualFile(root, "a.txt");
            child2Dir.GetItems().Add(file);
            VirtualFileSystem fs = new VirtualFileSystem(root);
            IFile found = fs.GetFile("/abc/def/a.txt");
            Assert.AreEqual(file, found);
        }
    }
}
